<template>
	<view>
		<view class="top-box"></view>
		<view class="search-box">
			<!-- <image src="/static/left.png" mode=""></image> -->
			<view class="input-box" style="width: 80%;" >
				<uni-icons type="search" size="20" color="#999" style="padding-right: 10upx;"></uni-icons>
				<input type="text" confirm-type="search" @confirm="search" v-model="searchValue" focus="true" placeholder="请输入关键词" placeholder-class="placeholder">
				<!-- <image src="/static/scan.png" mode=""></image> -->
			</view>
			<text @click="search">搜索</text>
		</view> 
		<view class="content">
			<view class="title">
				<text>历史搜索</text>
				<view class="clear-box" @click="handleOnDelete">
					<uni-icons type="trash" size="18" color="#999" style="padding-right: 2upx;"></uni-icons>
					<text>清空</text>
				</view>
			</view>
			<view class="child">
				<view class="item" v-for="(item, index) in localSearchList" :key="index" @click="handleSelectTag(item)">
					<text>{{item.value}}</text>
				</view>
			</view>
		</view>

	</view>
</template>

<script>
	const localSearchListKey = '__local_search_history'
	export default {
		data() {
			return {
				searchValue: '',
				localSearchList: uni.getStorageSync(localSearchListKey) || [],
				maxSearchListLength: 20,
				historyList: [{
					name: '瓜'
				}],
				hotList: [{
					name: '瓜',
					type: 1
				}],
				list: [{
					name: '只有衣服知道你的秘密',
					img: '',
					number: 1202
				}],
				tops: 0
			}
		},
		onLoad() {
			// console.log(this.localSearchList)
		},
		methods: {
			search() {
				if (this.searchValue) {
					const findIndex = this.localSearchList.findIndex(item => item.value === this.searchValue)
					if (findIndex !== -1) this.localSearchList.splice(findIndex, 1)
					if (this.localSearchList.length >= this.maxSearchListLength) this.localSearchList.pop()
					this.localSearchList.unshift({
						_createTime: new Date().getTime(),
						value: this.searchValue
					})
				} 
				this.navigateBackAndSetData(this.searchValue);
			},
			/**
			 * 选中、取消标签
			 */
			handleSelectTag(item) {
				this.searchValue = item.value;
				this.navigateBackAndSetData(item.value);
			},
			navigateBackAndSetData(searchValue){
				let pages = getCurrentPages(); // 当前页页⾯实例
				let nowPage = pages[pages.length -1]; //当前页⾯实例
				let prevPage = pages[pages.length -2]; // 上一页面实例
				// 需要返回 上一页的数据 Object
				let object ={
					keyWord: this.searchValue
				};
				prevPage.$vm.itemFilter(object) // 调用上一页 定义的方法
				uni.navigateBack()
				this.updateLocalStorage()
			},
			/**
			 * 点击删除按钮切换删除模式
			 */
			handleOnDelete() {
				this.localSearchList = [];
				this.updateLocalStorage()
			},
			/**
			 * 更新缓存
			 */
			updateLocalStorage() {
				uni.setStorageSync(localSearchListKey, this.localSearchList)
			},
			onShareAppMessage(){//点亮发送给朋友
			    return {
			      title: '帮助肝豆朋友们',//标题
			      path: '/pages/index/index'//路径
			    }
			},
			onShareTimeline() {}//点亮分享朋友圈,暂只支持Android平台
		}
	}
</script>

<style>
	page {
		background: #F6F6F6;
	}

	.top-box {
		height: 28upx;
	}

	.search-box {
		display: flex;
		/*  #ifndef MP-WEIXIN  */
		justify-content: space-between;
		/* #endif */
		align-items: center;
		margin: 0 24rpx;
		height: 44px;
		flex-direction: row;
	}

	.search-box image {
		width: 50rpx;
		height: 50rpx;
		flex-shrink: 0;
	}

	.search-box>text {
		flex-shrink: 0;
		color: #666;
	}

	.input-box {
		display: flex;
		align-items: center;
		background: #fff;
		height: 72rpx;
		border-radius: 72rpx;
		padding: 0 30rpx;
		/*  #ifndef MP-WEIXIN  */
		width: 80%;
		/* #endif */
		margin-right: 24rpx;
		flex-direction: row;
	}

	.input-box input {
		font-size: 28rpx;
		width: 100%;
	}

	.input-box .placeholder {
		color: #c6c6c6;
		font-size: 28rpx;
	}


	.input-box image {
		width: 48rpx;
		height: 48rpx;
	}

	.content {
		margin: 24rpx;
	}

	.title {
		display: flex;
		justify-content: space-between;
		align-items: center;
		margin: 24rpx 0;
		flex-direction: row;
	}

	.title>text {
		font-size: 30rpx;
		font-weight: bold;
	}

	.clear-box {
		color: #868686;
		font-size: 28rpx;
		display: flex;
		align-items: center;
		flex-direction: row;
	}

	.clear-box image {
		width: 32rpx;
		height: 32rpx;
		margin-right: 2rpx;
	}
	
	.clear-box text {
		font-size: 26rpx;
	}

	.child {
		display: flex;
		flex-wrap: wrap;
		flex-direction: row;
	}

	.child .item {
		background: #fff;
		font-size: 28rpx;
		height: 62rpx;
		border-radius: 54rpx;
		padding: 0 28rpx;
		color: #525252;
		margin: 0 24rpx 20rpx 0;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.child .item-a {
		padding: 0 28rpx 0 14rpx;
	}

	.child .item image {
		width: 40rpx;
		height: 40rpx;
		border-radius: 40rpx;
		margin-right: 10rpx;
	}

	.list {
		background: #fff;
		border-radius: 24rpx;
		padding: 15rpx 30rpx;
	}

	.list .item {
		display: flex;
		padding: 15rpx 0;
	}

	.item-info-box {
		display: flex;
		flex-direction: column;
		justify-content: space-evenly;
		color: #333333;

	}

	.item-info-box>text {
		-webkit-text-stroke-width: 0.1px;
		font-size: 30rpx;
	}

	.item-info-box>view {
		color: #FF4466;
		-webkit-text-stroke-width: 0.3px;
		font-size: 28rpx;
		display: flex;
		align-items: center;
	}

	.item-info-box>view image {
		width: 30rpx;
		height: 30rpx;
		margin-right: 4rpx;
	}

	.cover-box {
		position: relative;
	}

	.cover-box .label {
		position: absolute;
		top: 0;
		left: 0;
		width: 44rpx;
		height: 44rpx;
		z-index: 1;
		border-top-left-radius: 14rpx;
		border-bottom-right-radius: 14rpx;
		text-align: center;
		line-height: 44rpx;
		color: #fff;
		font-size: 28rpx;
		-webkit-text-stroke-width: 0.1px;
	}

	.label-0 {
		background: #E05345;
	}

	.label-1 {
		background: #EB9941;
	}

	.label-2 {
		background: #EDC744;
	}

	.label-3 {
		background: #696765;
	}

	.cover-box image {
		width: 120rpx;
		height: 120rpx;
		flex-shrink: 0;
		border-radius: 14rpx;
		margin-right: 30rpx;
	}
</style>